﻿
using com.paypal.sdk.services;
using com.paypal.soap.api;
using com.paypal.sdk.profiles;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


// PayPal API code samples: 
//  https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/library_code

// PayPal APIs Getting Started Guide
//  https://www.x.com/developers/paypal/documentation-tools/paypal-apis-getting-started-guide
// On how to make calls to the PayPal API operations.
// The components of an API request:
// Each PayPal API call contains of a combination of the following elements:
//   - API Credentials : API username + password + signature
//   - Service Endpoint : PayPal API server
//   - Call Payload : input fields for API calls
//   - Request and Response Format Types : SOAP or non-SOAP
//   - HTTP Headers

// TransactionSearch API Operation (SOAP)
//  https://www.x.com/developers/paypal/documentation-tools/api/transactionsearch-api-operation-soap
// GetTransactionDetails API Operation
//  https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_r_GetTransactionDetails

namespace PayPalAPITest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CallerServices caller = new CallerServices();
            IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();

            // Set up your API credentials, PayPal end point, and API version.
            profile.APIUsername = "wangzhi0417_api1.gmail.com";
            profile.APIPassword = "CRMYSWVBL5JT852U";
            profile.APISignature = "AQMtw.DvjboFcq4Zqmc4afh5R.KJAwS2C3Srh3Nu8ucV0p8D-rkZKmqO";
            profile.Environment = "live";
            caller.APIProfile = profile;

            // Create the request object.
			TransactionSearchRequestType concreteRequest = new TransactionSearchRequestType();
            concreteRequest.Version = "51.0";

            DateTime startDate = DateTime.Now.Subtract(new TimeSpan(2, 0, 0, 0));
            DateTime endDate = DateTime.Now;

            // Add request-specific fields to the request.
            concreteRequest.StartDate = startDate;
            concreteRequest.EndDate = endDate.AddHours(23).AddMinutes(59).AddSeconds(59);
            concreteRequest.EndDateSpecified = true;
            //concreteRequest.TransactionID = transactionId;

            // Execute the API operation and obtain the response.
            
            TransactionSearchResponseType pp_response = new TransactionSearchResponseType();
            pp_response = (TransactionSearchResponseType)caller.Call("TransactionSearch", concreteRequest);

            PaymentTransactionSearchResultType trans = pp_response.PaymentTransactions[0];
            string payer = trans.Payer;
            MessageBox.Show(payer);

            GetTransactionDetailsRequestType getTransReq = new GetTransactionDetailsRequestType();
            getTransReq.TransactionID = trans.TransactionID;

            GetTransactionDetailsResponseType transDetail = new GetTransactionDetailsResponseType();
            transDetail = (GetTransactionDetailsResponseType)caller.Call("GetTransactionDetails", getTransReq);

            MessageBox.Show(transDetail.PaymentTransactionDetails.PayerInfo.Address.Street1);

            PaymentTransactionType transType = transDetail.PaymentTransactionDetails;
            
            
        }
    }
}
